package dk.pun.dums.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;

import dk.pun.dums.client.DUMSServiceAsync;
import dk.pun.dums.client.event.CharacterDeletedEvent;
import dk.pun.dums.client.event.CharacterRenamedEvent;
import dk.pun.dums.client.event.CharacterSelectedEvent;
import dk.pun.dums.client.event.SelectCharacterEventHandler;
import dk.pun.dums.client.event.SystemMessageEventHandler;
import dk.pun.dums.client.event.SystemMessageUpdatedEvent;
import dk.pun.dums.client.view.MainView;
import dk.pun.dums.client.view.SelectCharacterView;
import dk.pun.dums.client.view.TopBarView;
import dk.pun.dums.client.view.ViewCharacterView;
import dk.pun.dums.client.view.ViewCharacterViewImpl;
import dk.pun.dums.shared.user.LoginInfo;

public class MainPresenter implements Presenter, MainView.Presenter, SelectCharacterEventHandler, SystemMessageEventHandler {

	private final transient MainView view;
	private final transient DUMSServiceAsync rpcService;

	public MainPresenter(final MainView view, final DUMSServiceAsync rpcService, final LoginInfo loginInfo) {
		this.view = view;
		this.rpcService = rpcService;
		
		if (this.view != null) {
			this.view.setPresenter(this);

			final TopBarView topBarView = this.view.getTopBarView();
			final TopBarPresenter topBarPresenter = new TopBarPresenter(topBarView);
			topBarPresenter.setSignedIn(loginInfo.getLogoutURL(), loginInfo.isAnonymous(), loginInfo.getUserData().getNickname());
			
			final SelectCharacterView selectCharacterView = this.view.getSelectCharacterView();
			new SelectCharacterPresenter(selectCharacterView, this, loginInfo.getUserData(), rpcService);

			this.view.setMessage(null);
			this.rpcService.getMessage(new AsyncCallback<String>() {
				
				@Override
				public void onSuccess(final String result) {
					view.setMessage(result);
				}
				
				@Override
				public void onFailure(final Throwable caught) {
					GWT.log("Failed to get system message: ", caught);
				}
			});
		}
	}
	
	@Override
	public void onSystemMessageUpdated(final SystemMessageUpdatedEvent event) {
		this.view.setMessage(event.getMessage());
	}
	
	@Override
	public void onCharacterSelected(final CharacterSelectedEvent event) {
		final ViewCharacterView viewCharacterView = new ViewCharacterViewImpl();
		new ViewCharacterPresenter(viewCharacterView, event.getCharacterData(), this.rpcService);
		this.view.addCharacter(viewCharacterView, event.getCharacterData().getName());
	}

	@Override
	public void onCharacterRenamed(final CharacterRenamedEvent event) {
		this.view.renameCharacter(event.getOldName(), event.getCharacterData().getName());
	}

	@Override
	public void onCharacterDeleted(final CharacterDeletedEvent event) {
		this.view.removeCharacter(event.getCharacterData().getName());
	}

	@Override
	public void go(final HasWidgets container) {
		container.clear();
		container.add(this.view.asWidget());
	}

}
